package com.stdspringboot.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stdspringboot.entity.Leave;
import com.stdspringboot.mapper.ILeaveMapper;
import com.stdspringboot.service.ILeaveService;
import com.stdspringboot.vo.LeaveVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class LeaveSrvImpl implements ILeaveService {

    @Resource
    private ILeaveMapper mapper;

    public PageInfo query(String applicant,int pageNum,int pageSize) {
        if(applicant!=null && !"".equals(applicant)){
            applicant=applicant+"%";
        }

        Page page= PageHelper.startPage(pageNum, pageSize, true);
        List list=mapper.query(applicant);
        //第一个参数：查询结果
        //第二个参数：总共的页数
        PageInfo pageInfo=new PageInfo(list,page.getPages());

        return pageInfo;
    }

    public int deleteById(String id) {

        return mapper.deleteById(id);

    }

    public int add(Leave leave) {

        Date date =new Date();
        leave.setCreateTime(date);
        leave.setModifyTime(date);
        int i=mapper.add(leave);

        return i;
    }

    public LeaveVO showById(String id) {
        return mapper.showById(id);
    }

    public int modifyById(LeaveVO leave) {
        //将修改日期传递给leave对象，更新到数据库的modify_time 字段
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr=sdf.format(date);//将date对象转换成字符串，安装上面的格式
        leave.setModifyTime(dateStr);
        return mapper.modifyById(leave);

    }
}
